package com.huawei.petSystemdemo;


import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.bean.User;
import com.huawei.petSystemdemo.ui.my.MyFragment;
import com.huawei.petSystemdemo.ui.notifications.NotificationsFragment;
import com.huawei.petSystemdemo.utils.MyRequest;
import com.huawei.petSystemdemo.utils.Request;


import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 登录界面类
 *
 * 2021.6.30
 */

public class LoginActivity extends AppCompatActivity {
    EditText EtStuNumber,EtStuPwd;
    private String username;
    private String password;
    LinkedList<User> users = new LinkedList<>();
    private MyApplication myApplication;


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.load);

        //myApplication = (MyApplication)getApplication();

        TextView tvRegister = findViewById(R.id.register2);
        //跳转到注册界面
        tvRegister.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivity(intent);
        });

        EtStuNumber = getDelegate().findViewById(R.id.editTextTextPersonName1);
        EtStuPwd = getDelegate().findViewById(R.id.editTextTextPassword1);
        Button btnLogin = findViewById(R.id.LoginByUsername);
        ImageView HuaWeiLogin = findViewById(R.id.LoginByHuawei);

        //登录按钮被点击
        btnLogin.setOnClickListener((v -> {
            boolean flag = false;
            final int[] a = {0};
            Intent intent1 = new Intent(this,DelayMessageService.class);
            intent1.putExtra(DelayMessageService.EXTRA_MESSAGE,getResources().getString(R.string.SuccessMessage));
            if (ChickInput()){
                //MyRequest.baseUrl = "http://10.0.2.2:8080/front";//AS自带模拟器通用端口
                //MyRequest.baseUrl = "http://192.168.42.1:8080/front/";//每次记得修改
                //MyRequest.baseUrl = Request.url;//在util/Request.java修改整体的地址

                Map<String,Object> requestBody = new HashMap<>();
                requestBody.put("username",EtStuNumber.getText().toString());
                requestBody.put("password",EtStuPwd.getText().toString());




                MyRequest.post("/user/login", requestBody, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        //Log.e("tag","msg");
                        Looper.prepare();
                        System.out.println("连接服务器失败!");
                        Toast.makeText(LoginActivity.this,"连接服务器失败!",Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {


                        String json = response.body().string();
                        JSONObject jsonObject = JSON.parseObject(json);
                        int statusCode = jsonObject.getInteger("statusCode");
                        //JSONObject data = jsonObject.getJSONObject("data");

                        if(statusCode == 200) {//第一次登录传递信息
                            a[0] =statusCode;
                            System.out.println(a[0]+"a");
                            Looper.prepare();//增加部分
                            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                            System.out.println("登录成功");

                            startService(intent1);

                            User user = jsonObject.getObject("data", User.class);
                            GlobalViewModel.getGlobal().user = user;

//                            Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
//                            intent.putExtra("flag", 1);
//                            startActivity(intent);

                            setResult(10);
                            finish();


                            Looper.loop();
                        }
                        else {
                            System.out.println("密码错误");
                            Looper.prepare();//增加部分
                            Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    }
                });
            }
            if(a[0]==200){
                System.out.println(a[0]+"a[0]");
                startService(intent1);
            }
        }));

        //华为服务登录
        HuaWeiLogin.setOnClickListener(v->{
            Intent intentHMS = new Intent(LoginActivity.this,AuthWithAuthCodeActivity.class);
            startActivity(intentHMS);
        });

    }

    public boolean ChickInput(){
        String StuNumber = EtStuNumber.getText().toString();
        String StuPwd = EtStuPwd.getText().toString();
        //trim() 方法用于删除字符串的头尾空白符
        if(StuNumber.trim().equals("")) {
            Toast.makeText(LoginActivity.this,"用户名不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(StuPwd.trim().equals("")) {
            Toast.makeText(LoginActivity.this,"密码不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == 2) {
//            if (requestCode == 1) {
//
//                //设置结果显示框的显示数值
////                name.setText(getGlobal().user.getUsername());
////                integral.setText(getGlobal().user.getIntegral() + "");
//                System.out.println("ffffffffffffffffffffffffffffffffffffffff");
//            }
//        }
//    }



}
